from rest_framework.decorators import action
from rest_framework.viewsets import ReadOnlyModelViewSet
from meiduo_admin.my_paginate import MyPageNumberPagination
from orders.models import OrderInfo, OrderGoods
from meiduo_admin.serializer import orders_serializer
from rest_framework.response import Response

class OrdersView(ReadOnlyModelViewSet):
    pagination_class = MyPageNumberPagination
    serializer_class = orders_serializer.OrdersSerializer
    # queryset = OrderInfo.objects.all()

    def get_queryset(self):
        keyword = self.request.query_params.get("keyword")
        if keyword:
            return OrderInfo.objects.filter(order_id__contains=keyword)
        else:
            return OrderInfo.objects.all()

    @action(methods=['PUT'], detail=True)
    def status(self, request, pk):
        status = request.data.get('status')
        order = self.get_object()

        if not status:
            return Response(status=400)

        order.status= status
        order.save()

        return Response(status=200)




